from setuptools import setup, find_packages
import os, os.path, sys, glob
sys.path.insert(0, 'src')
import Text2Xml
module_dir    = os.path.abspath(os.path.dirname(Text2Xml.__file__))
python_module = os.path.join(module_dir, 'pytextparser.so')
c_dir         = os.path.join(module_dir, 'CGenerator')
gen_files     = glob.glob(os.path.join(c_dir, '*.[ch]'))
type_files    = glob.glob(os.path.join(c_dir, 'types',  '*'))
parser_files  = glob.glob(os.path.join(c_dir, 'parser', '*'))
remove1       = glob.glob(os.path.join(c_dir, 'types',  'tests'))[0]
remove2       = glob.glob(os.path.join(c_dir, 'parser', 'tests'))[0]
type_files.remove(remove1)
parser_files.remove(remove2)

def precompile():
    print "Compiling pytextparser.so..."
    generator     = Text2Xml.CGenerator()
    generator._compile_libs()
    generator._compile_python_module(python_module)

    oldwd = os.getcwd()
    for lib in ('types', 'parser'):
        print "Compiling %s..." % lib
        os.chdir(os.path.join(c_dir, lib))
        if os.system('make') != 0:
            raise Exception('Precompilation failed')
    os.chdir(oldwd)

precompile()
setup(name         = 'Text2Xml',
      version      = '1.0',
      description  = 'Parses text files, and converts them to XML.',
      long_description = '''
A combined parser builder and XML generator.
''',
      author       = 'Samuel Abels',
      author_email = 'http://debain.org',
      license      = 'lGPLv2',
      packages     = [p for p in find_packages('src')],
      package_dir  = {'': 'src'},
      scripts      = ['text2xml'],
      data_files   = [('Text2Xml',                   [python_module]),
                      ('Text2Xml/CGenerator',        gen_files),
                      ('Text2Xml/CGenerator/types',  type_files),
                      ('Text2Xml/CGenerator/parser', parser_files)],
      requires     = [],
      keywords     = 'parser lexer xml generator syntax text text2xml',
      url          = 'http://www.debain.org/',
      #ext_modules  = [pytextparser],
      classifiers  = [
        'Development Status :: 5 - Production/Stable',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)',
        'Programming Language :: Python',
        'Topic :: Other/Nonlisted Topic',
        'Topic :: Software Development :: Libraries',
        'Topic :: Software Development :: Libraries :: Python Modules'
      ])
